package cn.IT.service.impl;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.IT.dao.IApproveDAO;
import cn.IT.dao.IKnowledgeDAO;
import cn.IT.dao.ITopicDAO;
import cn.IT.dao.IUserDAO;
import cn.IT.domain.Approve;
import cn.IT.domain.Knowledge;
import cn.IT.domain.Reply;
import cn.IT.domain.Topic;
import cn.IT.domain.User;
import cn.IT.service.IApproveService;
import cn.IT.service.ITopicService;
import cn.IT.service.IUserService;
import net.sf.json.JSONObject;

@Service(value = "approveService")
@SuppressWarnings("all")
@Transactional
public class ApproveService implements IApproveService {

	@Resource
	private IApproveDAO approveDAO;
	@Resource
	private IKnowledgeDAO knowledgeDAO;
	@Resource
	private ITopicDAO topicDAO;
	@Resource
	private IUserDAO userDAO;


	@Override
	public List<Approve> getAllUnapprovedByApprover(User user) {
		List<Approve> approve = approveDAO.getAllUnapprovedByApprover(user);
		if(approve != null){
			Collections.reverse(approve);
		}
		return approve;
	}

	@Override
	public List<Approve> getAllApproveByUser(User user) {
		List<Approve> approve = approveDAO.getAllApproveByUser(user);
		if(approve != null){
			Collections.reverse(approve);
		}
		return approve;
	}

	@Override
	public List<Approve> getAllApprovedByApprover(User user) {
		List<Approve> approve = approveDAO.getAllApprovedByApprover(user);
		if(approve != null){
			Collections.reverse(approve);
		}
		return approve;
	}

	@Override
	public Approve getApprovedByTopic(Topic topic) {
		return approveDAO.getApprovedByTopic(topic);
	}

	@Override
	public String addApprove(Approve approve) {
		try {
			approve.setStatus("待审核");
			approve.setCreatedOn(new Date());
			approveDAO.save(approve);
			return "success";
		} catch (Exception e) {
			System.out.println(e);
			return "error";
		}
	}
	
	@Override
	public Approve getApproveById(Serializable approveId) {
		return approveDAO.get(Approve.class, approveId);
	}
	@Override
	public String updateApprove(Approve approve,User user) {
		Approve updateApprove = approveDAO.get(Approve.class, approve.getApproveId());
		try {
			updateApprove.setUpdateTime(new Date());
			updateApprove.setStatus(approve.getStatus());
			approveDAO.update(updateApprove);
			if (approve.getStatus().equals("审核通过") ){
				Knowledge knowledge = new Knowledge();
				knowledge.setType(updateApprove.getTopic().getType());
				knowledge.setAnswer(updateApprove.getAnswer());
				knowledge.setCreatedOn(new Date());
				knowledge.setQuestion(updateApprove.getTopic().getTitle());
				knowledge.setUser(user);
				knowledgeDAO.save(knowledge);
				
			    User updateUser = userDAO.get(User.class, user.getUserId());
				updateUser.setContributionCount(updateUser.getContributionCount()+1);
				userDAO.update(updateUser);
			}
			return "success";
		} catch (Exception e) {
			System.out.println(e);
			return "error";
		}
	}
	
	
	


}
